<template>
	<view class="uni_box home">
		<navbar title='语音广告'>
		</navbar>
		<view class="content">

		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				resouceId:'',
				userId:'',
			}
		},
		onLoad() {
			this.getinit()
		},
		onShow() {
		},
		methods: {
			async getinit(){
				
				let res = await this.$u.api.voicedata({
				
				});
				console.log(res)
				const voicdeReadSdk = uni.requireNativePlugin('xlx-voice-read-module');
					voicdeReadSdk.init({
					appId: res.appid,//'43516296',//渠道appId，从配置后台获取
					appSecret: res.appsecret,//'RxnMEW5hKcwJE8FvnFkJtYdl20xU6vL', //密钥，从配置后台获取
					debug: false
				});
				voicdeReadSdk.loadVoiceAd({
					resourceId: res.resource_id, //必要，资源位id
					userId: res.user_id, //可选，用于服务端回调时通知接入方给哪个用户发放奖励，不走服务端回调非必要
					mediaExtra: "", //可选，额外的透传参数，仅支持单个json对象格式，不可以嵌套json对象
					nickname: "" //可选，用户昵称
				}, result => {
					console.log("result = " + JSON.stringify(result));
					switch (result.eventType) {
						//广告加载成功(注：此时只是广告加载成功，并没有展示。如需直接展示还需调用 showVoiceAd 方法)
						case "onAdLoadSuccess":
							// preEcpm 千次展现预估收益，单次展现收益需要除1000
							this.preEcpm = result.params.preEcpm;
							break;
						// 广告加载失败
						case "onAdLoadError":
							var errorCode = result.params.errorCode;
							var errorMsg = result.params.errorMsg;
							//请务必给用户展示错误信息，方便用户反馈时排查问题
							uni.showToast({
								icon: 'none',
								title: `${errorMsg}(${errorCode})`,
								duration: 2000
							});
							break;
					} 
				})

				voicdeReadSdk.showVoiceAd(result => {
					console.log("result = " + JSON.stringify(result));
					switch (result.eventType) {
						case "onAdShow":
							//语音展示完成
							break;
						case "onAdError":
							var errorCode = result.params.errorCode;
							//广告展示失败
							break;
						case "onAdClose":
							console.log('关闭广告')
							setTimeout(() => {
								uni.navigateBack()
							}, 1000);
							//广告关闭
							break;
						/**
						 * 奖励验证回调，开发者需要在此回调中做奖励的发放
						 * 多个奖励则会回调多次
						 */
						case "onRewardVerify":
							//requestId 当前曝光的请求id
							var requestId = result.params.requestId;
							// tagId 奖励唯一标识
							var tagId = result.params.tagId;
							//rewardAmount 当次广告媒体收益（单位元）
							var rewardAmount = result.params.rewardAmount;
							break;
					} 
					})
				
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background-color: #F5F5F5;
	}
	.content{
		background-color: #F5F5F5;
		min-height: 100vh;
	}
</style>
